package com.leoman.plz_p.views.homepage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.CommentAdapter;
import com.leoman.plz_p.constants.Constants;
import com.leoman.plz_p.interfaces.PointDetailView;
import com.leoman.plz_p.module.bean.CommentBean;
import com.leoman.plz_p.module.bean.PointDetail;
import com.leoman.plz_p.presenter.PointDetailPresenter;
import com.leoman.plz_p.utils.FastBlurUtil;
import com.leoman.plz_p.utils.authority.AuthorityContext;
import com.leoman.plz_p.weidget.NavBar;
import com.leoman.plz_p.weidget.RoundedTransformationBuilder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.corelib.base.BaseActivity;
import cn.com.corelib.utils.DisplayUtil;
import cn.com.corelib.utils.ToastMgr;

/**
 * Created by guoh on 2018/9/3.
 */
public class PointDetailActivity extends BaseActivity<PointDetailView,PointDetailPresenter>implements PointDetailView{
    private String courseId;
    private String courseChapterId;
    private String missionClassId;

    public static Intent getLaunchIntent(Context context, String courseId, String courseChapterId) {
        Intent intent = new Intent(context, PointDetailActivity.class);
        intent.putExtra(Constants.KEY_COURSE_ID, courseId);
        intent.putExtra(Constants.KEY_COURSE_CHAPTER_ID, courseChapterId);
        return intent;
    }

    public static Intent getLaunchIntent(Context context,String missionClassId) {
        Intent intent = new Intent(context, PointDetailActivity.class);
        intent.putExtra(Constants.KEY_MISSION_CLASS_ID, missionClassId);
        return intent;
    }

    @Bind(R.id.et_comment)
    EditText etComment;
    @Bind(R.id.tv_comment_now)
    TextView tvCommentNow;
    @Bind(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.nav)
    NavBar nav;
    @Bind(R.id.ls_commit)
    ListView lsCommit;

    private CommentAdapter adapter;

    private int pageIndex = 1;

    private View view;

    private ViewHolder viewHolder;

    private String videoUrl;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_point_detail;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        view = LayoutInflater.from(getViewContext()).inflate(R.layout.view_header, null);
        viewHolder=new ViewHolder(view);
        courseId = getIntent().getStringExtra(Constants.KEY_COURSE_ID);
        courseChapterId = getIntent().getStringExtra(Constants.KEY_COURSE_CHAPTER_ID);
        missionClassId=getIntent().getStringExtra(Constants.KEY_MISSION_CLASS_ID);

        nav.setTitle("知识点");
        adapter = new CommentAdapter(getViewContext());
        lsCommit.addHeaderView(view);
        lsCommit.setAdapter(adapter);

        initRefresh();
        if (missionClassId==null) {
            getPresenter().getPointDetail(courseId, courseChapterId);
            getPresenter().getComment(courseChapterId, pageIndex, 12);
        }else {
            getPresenter().getPointDetailByMissionClassId(missionClassId);
            getPresenter().getCommentByMissionClassId(missionClassId,pageIndex,12);
        }

        viewHolder.tvStudyNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(VideoPlayActivity.getLaunchIntent(getViewContext(),courseId,courseChapterId,videoUrl));
            }
        });
        viewHolder.ivPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(VideoPlayActivity.getLaunchIntent(getViewContext(),courseId,courseChapterId,videoUrl));
            }
        });
    }

    @Override
    protected PointDetailPresenter createPresenter() {
        return new PointDetailPresenter();
    }


    @OnClick({R.id.tv_comment_now})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_comment_now:
                if (AuthorityContext.getContext().checkAuthority(getViewContext())) {
                    if (isEmpty(etComment)) {
                        ToastMgr.show("请输入评论");
                        return;
                    }
                    if (missionClassId == null) {
                        getPresenter().sendComment(courseChapterId, getText(etComment));
                    } else {
                        getPresenter().sendCommentByMissionClassId(missionClassId, getText(etComment));
                    }
                }
                break;
        }
    }

    private void initRefresh() {
        refreshLayout.setEnableLoadmoreWhenContentNotFull(false);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex += 1;
                getPresenter().getComment(courseChapterId, pageIndex, 12);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

            }
        });
    }

    @Override
    public void obtainPointDetail(final PointDetail data) {
        viewHolder.tvNameChinese.setText(data.getName());
        viewHolder.tvPointContent.setText(Html.fromHtml(data.getContent()));
        videoUrl=data.getUrl();
        if (!TextUtils.isEmpty(data.getImage())) {
            Glide.with(getViewContext()).load(data.getImage()).centerCrop().transform(new CenterCrop(getViewContext()), new RoundedTransformationBuilder()
                    .cornerRadius(DisplayUtil.dip2px(getViewContext(), 5)).build(getViewContext())).into(viewHolder.ivThumb);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int scaleRatio = 10;
                    final Bitmap blurBitmap = FastBlurUtil.GetUrlBitmap(data.getImage(), scaleRatio);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.ivBr.setImageBitmap(blurBitmap);
                        }
                    });

                }
            }).start();
        }
    }

    @Override
    public void obtainCommentList(List<CommentBean> rows) {
        if (rows!=null&&rows.size()>0) {
            if (pageIndex==1){
                adapter.clear();
            }
            adapter.addAll(rows);
            refreshLayout.finishLoadmore(true);
        }else {
            refreshLayout.finishLoadmoreWithNoMoreData();
        }
    }

    @Override
    public void obtainCommentSuccess() {
        ToastMgr.show("评论成功");
        pageIndex = 1;
        getPresenter().getComment(courseChapterId, pageIndex, 12);
    }


    static class ViewHolder {
        @Bind(R.id.iv_br)
        ImageView ivBr;
        @Bind(R.id.iv_thumb)
        ImageView ivThumb;
        @Bind(R.id.iv_play)
        ImageView ivPlay;
        @Bind(R.id.tv_name_chinese)
        TextView tvNameChinese;
        @Bind(R.id.tv_name_english)
        TextView tvNameEnglish;
        @Bind(R.id.tv_study_now)
        TextView tvStudyNow;
        @Bind(R.id.tv_point_content)
        TextView tvPointContent;



        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
